package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.constance.MessageEvent;
import com.typy.www.tpay.util.CheckUtil;
import com.typy.www.tpay.util.MessageAction;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;
import com.zhy.android.percent.support.PercentLinearLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 13799 on 2018/6/14.
 */

public class ChangePasswordActivity extends BaseBackActivity {


    @BindView(R.id.et_phonenumber)
    AutoCompleteTextView etPhonenumber;
    @BindView(R.id.btn_yanzhengma)
    TextView btnYanzhengma;
    @BindView(R.id.ll_yanzhengma)
    PercentLinearLayout llYanzhengma;
    @BindView(R.id.et_newPassWord)
    AutoCompleteTextView etNewPassWord;
    @BindView(R.id.btn_queding)
    TextView btnQueding;
    @BindView(R.id.tv_wangjimima)
    TextView tvWangjimima;
    @BindView(R.id.et_yanzhengma)
    AutoCompleteTextView etYanzhengma;
    @BindView(R.id.et_oldPassWord)
    AutoCompleteTextView etOldPassWord;
    @BindView(R.id.et_oldPayPassWord)
    AutoCompleteTextView etOldPayPassWord;
    @BindView(R.id.et_newPayPassWord)
    AutoCompleteTextView etNewPayPassWord;
    private int classType;
    private String nickName;

    public static void show(Context context, int type) {
        Intent intent = new Intent();
        intent.putExtra("type", type);
        IntentUtil.intent2Activity(context, intent, ChangePasswordActivity.class);
    }
    public static void show(Context context, int type,String nickName) {
        Intent intent = new Intent();
        intent.putExtra("type", type);
        intent.putExtra("nickName", nickName);
        IntentUtil.intent2Activity(context, intent, ChangePasswordActivity.class);
    }
    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        classType = getIntent().getIntExtra("type", 0);
        nickName=getIntent().getStringExtra("nickName");
        updateTitile();
    }

    private void updateTitile() {
        if (Constance.CHANGENICKNAME == classType) {
            setTitle("修改昵称");
            llYanzhengma.setVisibility(View.GONE);
            etNewPassWord.setVisibility(View.GONE);
            etPhonenumber.setInputType(InputType.TYPE_CLASS_TEXT);
            etPhonenumber.setHint("请输入昵称");
            tvWangjimima.setVisibility(View.INVISIBLE);
            etOldPassWord.setVisibility(View.GONE);
            etOldPayPassWord.setVisibility(View.GONE);
            etPhonenumber.setText(nickName);
        } else if (Constance.CHANGEPASSWORD == classType) {
            llYanzhengma.setVisibility(View.GONE);
            etOldPassWord.setVisibility(View.VISIBLE);
            etOldPayPassWord.setVisibility(View.GONE);
            tvWangjimima.setVisibility(View.VISIBLE);
            etPhonenumber.setVisibility(View.GONE);
            etNewPassWord.setVisibility(View.VISIBLE);
            etNewPayPassWord.setVisibility(View.GONE);
            setTitle("修改登录密码");
        } else if (Constance.CHANGEPAYPASSWORD == classType) {
            llYanzhengma.setVisibility(View.GONE);
            etOldPassWord.setVisibility(View.GONE);
            etOldPayPassWord.setVisibility(View.VISIBLE);
            etNewPayPassWord.setVisibility(View.VISIBLE);
            etNewPassWord.setVisibility(View.GONE);
            tvWangjimima.setVisibility(View.VISIBLE);
            etPhonenumber.setVisibility(View.GONE);
            setTitle("修改支付密码");
        }
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_forget;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
    }

    @OnClick({R.id.tv_wangjimima, R.id.btn_queding})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_wangjimima:
                if (Constance.CHANGEPASSWORD == classType) {
                    ForgetPassWordActivity.show(getActivity(), Constance.REFINDPASSWORD);
                } else {
                    ForgetPassWordActivity.show(getActivity(), Constance.REFINDPAYPASSWORD);
                }
                break;
            case R.id.btn_queding:
                save();
                break;
        }
    }

    private void save() {
        if (Constance.CHANGENICKNAME == classType) {
            if (CheckUtil.checkText(getActivity(), etPhonenumber)) {
                updataNickName();
            }
        } else if (Constance.CHANGEPASSWORD == classType) {
            if (CheckUtil.checkText(getActivity(), etOldPassWord, etNewPassWord)) {
                if(CheckUtil.checkText(getActivity(),"请输入至少6位登录密码",6,etNewPassWord))
                updataPassWord();
            }
        } else if (Constance.CHANGEPAYPASSWORD == classType) {
            if (CheckUtil.checkText(getActivity(), etOldPayPassWord, etNewPayPassWord)) {
                if(CheckUtil.checkText(getActivity(),"请输入至少6位支付密码",6,etNewPayPassWord))
                updataPayPassWord();
            }
        }
    }

    private void updataNickName() {
        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.put("fdNickName", etPhonenumber);
        HTTPUtils.post(getActivity(), Constance.getUpdatanicknameURL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                UiHelper.showToast(getActivity(), ret.getMsg(), true);
                EventBus.getDefault().post(new MessageEvent(MessageAction.REFRESHUSERINFO));
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    private void updataPassWord() {
        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.putMd5("fdOldPwd", etOldPassWord);
        paramsUtil.putMd5("fdPwd", etNewPassWord);
        EventBus.getDefault().post(new MessageEvent(MessageAction.REFRESHUSERINFO));
        HTTPUtils.post(getActivity(), Constance.getUpdatapasswordURL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                UiHelper.showToast(getActivity(), "密码修改成功！", true);
                EventBus.getDefault().post(new MessageEvent(MessageAction.REFRESHUSERINFO));
                finish();
            }

            //
            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });

    }

    private void updataPayPassWord() {
        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.putMd5("fdOldPayPwd", etOldPayPassWord);
        paramsUtil.putMd5("fdPayPwd", etNewPayPassWord);
        EventBus.getDefault().post(new MessageEvent(MessageAction.REFRESHUSERINFO));
        HTTPUtils.post(getActivity(), Constance.getUpdatapaypasswordURL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                UiHelper.showToast(getActivity(), "支付密码修改成功！", true);
                EventBus.getDefault().post(new MessageEvent(MessageAction.REFRESHUSERINFO));
                finish();
            }

            //
            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }


}
